<?php


namespace App\Logic;

use App\Models\Fang;
use App\Models\Member;
use App\Models\User;
use App\server\JwtServer;
use App\server\RedisServer;
use Illuminate\Support\Facades\Cache;
use App\server\SMS\CodeServer;
/**
 * 后台用户信息逻辑层
 * Class AdminLogic
 * @package App\Logic
 *  @ name:zf
 */
class AdminLogic{

    /**
     * Undocumented function
     * 列表
     * @param [type] $name
     * @param [type] $phone
     * @return void
     */
    public static function AdminList($paginate){
        if(empty($paginate)){
            $paginate=10;
        }
        return User::paginate($paginate);
    }
    /**
     * Undocumented function
     * 列表
     * @param [type] $name
     * @param [type] $phone
     * @return void
     */
    public static function AdminSearch($name,$phone,$paginate){
        if(!empty($name)){
            $where[]=['truname','like',"%$name%"];
        }
        if(!empty($phone)){
            $where[]=['phone','like',"%$phone%"];
        }
        if(empty($paginate)){
            $paginate=10;
        }
        if(!empty($where)){
            return User::adminList($where,$paginate);
        }
        return User::paginate($paginate);
    }
     /**
     * Undocumented function
     * 详情
     * @param [type] $id
     * @return void
     */
    public static function adminDetail($user,$vid){
        if(!empty($user)){
            $id=JwtServer::JwtDecode($user);
        }
        if(!empty($vid)){
           $id=$vid;
        }
        $user=User::adminDetail(['id'=>$id]);
        if(empty($user))throw new \Exception('该用户不存在');
        return $user;
    }
     /**
     * Undocumented function
     * 添加用户信息
     * @param [type] $id
     * @return void
     */
    public static function adminSave($admin,$request){
        $request->validate([
            'phone' =>'required|unique:zfm_users|regex:/^1[^0-2]\d{9}$/',
            'email'=>'required|email|unique:zfm_users',
            'username'=>'required|unique:zfm_users',
            'truname'=>'required|unique:zfm_users',
        ]);
        $admin['password']=bcrypt($admin['password']);
           $user= User::create($admin);
           return $user;
    }
    /**
     * Undocumented function
     * 编辑用户信息
     * @param [type] $id
     * @return void
     */
    public static function adminUpdate($admin,$request){
        $request->validate([
            'phone' =>'required|regex:/^1[^0-2]\d{9}$/',
            'email'=>'required|email',
            'username'=>'required',
            'truname'=>'required',
        ]);
        $admin['password']=bcrypt($admin['password']);
        return User::where('id',$admin['id'])->update($admin);
    }
    /**
     * 用户登录注册
     */
    public static function adminLogin($key,$request){

        $res=self::verifyCode($key,$request);
        if($res!=true)throw new \Exception('验证失败');
        $user=User::where('phone',$key['phone'])->first();
        if(empty($user)){
            $user_id=User::create(['phone'=>$key['phone']]);
            $token=JwtServer::jwtEncode($user_id);
        }else{
            $token=JwtServer::jwtEncode($user['id']);
        }
        return $token;
    }
    /**
     * 用户绑定手机号
     */
    public static function adminPhone($key,$request){

        $res=self::verifyCode($key,$request);
        if($res!=true)throw new \Exception('验证失败');
        $id=JwtServer::JwtDecode($key['user']);
        $user=User::where('id',$id)->update(['phone'=>$key['phone']]);
        return $user;
    }
     /**
     * 手机号发送验证码
     */
    public static function sendCode($phone,$type,$request){
        $request->validate([
            'tel' =>'required|regex:/^1[^0-2]\d{9}$/',
            'type'=>'required',
        ]);
        $key=$phone;
        $redis=RedisServer::createRedisObject();
        $redis->select(1);
          //判断当前用户一分钟内是否发送过短信
        $val=$redis->GetRedis($key);
        if(!empty($val)) throw new \Exception('短信已发送，请稍后再试');
        $redis->setex($key,60,$phone);
        $code=CodeServer::sendCode($phone,$type);
        return $code;
    }
    //验证验证码
    public static function verifyCode($key,$request){
        $request->validate([
            'phone' => 'required|regex:/^1[^0-2]\d{9}$/',
            'code' => 'required'
        ]);
        //$key=$key['phone'];
        // $codekey=$key.'code';
        // $redis=RedisServer::createRedisObject();
        // $redis->select(1);
        $phone= Cache::get('phone');
        return $phone;
        $code=Cache::get('code');
        if(empty($phone))throw new \Exception('验证码已失效，请重新发送');
        if($key['phone']!=$phone)throw new \Exception('手机号不正确');
        if($key['code']!=$code)throw new \Exception('验证码不正确');
        return true;
    }
     /**
     * 用户会员权限
     */
    public static function adminMember($key){
        if(!empty($key['id'])){
            $id = $key['id'];
        }else{
            $id=JwtServer::JwtDecode($key['user']);
        }
        $user= User::adminDetail(['id'=>$id]);
        $members_ids=explode(',',$user['members_ids']);
        $member=Member::whereIn('id',$members_ids)->get();
        return $member;
    }
    /**
     * Undocumented function
     * 删除
     * @param [type] $id
     * @return void
     */
    public static function adminDel($vid){
        if(!empty($vid)){
           $id=$vid;
        }
        return User::adminDel(['id'=>$id]);
    }
}
